// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Burrobot Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 46 : TYPE_BURROBOT

// Function declarations
reserve function Burrobot_DebugDraw
reserve function Burrobot_DebugSpawn

// Static Values

// Tables

function Burrobot_DebugDraw
	DrawSpriteFX(3, FX_FLIP, object.xpos, object.ypos)
end function


function Burrobot_DebugSpawn
	CreateTempObject(TypeName[Burrobot], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	if object[tempObjectPos].direction == FLIP_NONE
		object[tempObjectPos].xvel = -0x8000
	else
		object[tempObjectPos].xvel = 0x8000
	end if
	object[tempObjectPos].value0 = object.xpos
	object[tempObjectPos].value1 = object.ypos
	object[tempObjectPos].yvel = -0x40000
	object[tempObjectPos].animation = 1
	object[tempObjectPos].state = 1
end function


event ObjectMain
	switch object.state
	case 0
		object.value4 = 0x7FFFFFFF
		object.value2 = 0
		object.value3 = 0xFFFF
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].xpos
			temp0 -= object.xpos
			Abs(temp0)
			if temp0 < object.value4
				object.value4 = temp0
				object.value2 = currentPlayer
			end if
			if temp0 < 0x600000
				if object[currentPlayer].ypos <= object.ypos
					if object.value3 != 0xFFFF
						arrayPos0 = object.value3
						if object[currentPlayer].ypos < object[arrayPos0].ypos
							object.value3 = currentPlayer
						end if
					else
						object.value3 = currentPlayer
					end if
				end if
			end if
		next
		if object.value3 != 0xFFFF
			arrayPos0 = object.value3
			temp0 = object.ypos
			temp0 -= object[arrayPos0].ypos
			if temp0 >= 0
				if temp0 <= 0x800000
					Rand(temp0, 8)
					if temp0 == 0
						object.state = 1
						object.priority = PRIORITY_ACTIVE
						if object[arrayPos0].xpos < object.xpos
							object.direction = FLIP_NONE
							object.xvel = -0x8000
						else
							object.direction = FLIP_X
							object.xvel = 0x8000
						end if
						object.yvel = -0x40000
						object.animation = 1
					end if
				end if
			end if
		else
			arrayPos0 = object.value2
			if object[arrayPos0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
		end if
		break
	case 1
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x1800
		if object.yvel > 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 20, 0)
			if checkResult == 1
				arrayPos0 = object.value2
				if object[arrayPos0].xpos < object.xpos
					object.direction = FLIP_NONE
				else
					object.direction = FLIP_X
				end if
				object.value5 = 256
				object.animation = 2
				object.value6 = 0
				object.state = 2
			end if
		end if
		break
	case 2
		object.xpos += object.xvel
		temp0 = 0
		object.value5--
		if object.value5 == 0
			temp0 = 1
		end if
		if object.direction == FLIP_NONE
			ObjectTileCollision(CSIDE_FLOOR, -12, 21, 0)
			if checkResult == 0
				ObjectTileCollision(CSIDE_FLOOR, -12, 19, 0)
				if checkResult == 0
					ObjectTileCollision(CSIDE_FLOOR, -12, 20, 0)
				end if
			end if
		else
			ObjectTileCollision(CSIDE_FLOOR, 12, 21, 0)
			if checkResult == 0
				ObjectTileCollision(CSIDE_FLOOR, 12, 19, 0)
				if checkResult == 0
					ObjectTileCollision(CSIDE_FLOOR, 12, 20, 0)
				end if
			end if
		end if
		if checkResult == 0
			temp0 = 1
		end if
		ObjectTileGrip(CSIDE_FLOOR, 0, 20, 0)
		if temp0 == 1
			GetBit(temp0, oscillation, 2)
			if temp0 == 0
				object.yvel = -0x40000
				object.animation = 1
				object.state = 1
			else
				object.value5 = 60
				object.animation = 3
				object.state = 3
			end if
		end if
		break
	case 3
		object.value5--
		if object.value5 == 0
			FlipSign(object.xvel)
			if object.xvel < 0
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			object.value5 = 256
			object.animation = 2
			object.state = 2
		end if
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value0
		object.ypos = object.value1
		if object.outOfBounds == 1
			object.xpos = object.value0
			object.ypos = object.value1
			object.xvel = 0
			object.yvel = 0
			object.animation = 0
			object.animationTimer = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.value6 = 1
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
	foreach (TypeName[Bullet], arrayPos0, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, arrayPos0, -6, -4, 6, 4)
		if checkResult == 1
			CallFunction(BulletKill)
		end if
	next	
	object.frame = object.animationTimer
	object.frame >>= 2
	switch object.animation
	case 0
	case 1
		if object.yvel > 0
			object.frame = 5
		else
			object.frame += 3
		end if
		break
	case 2
		break
	case 3
		object.frame++
		break
	end switch
	object.animationTimer++
	object.animationTimer &= 7
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == 1
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 34, 68)
		SpriteFrame(-16, -16, 28, 36, 63, 68)
		SpriteFrame(-16, -16, 28, 36, 194, 90)
		SpriteFrame(-12, -24, 24, 46, 92, 68)
		SpriteFrame(-12, -24, 24, 46, 143, 90)
		SpriteFrame(-16, -20, 25, 42, 168, 90)
		SpriteFrame(-24, -12, 38, 24, 118, 65)
	end if
	CheckCurrentStageFolder("BossRush")
	if checkResult == 1
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 1, 227)
		SpriteFrame(-16, -16, 28, 36, 30, 227)
		SpriteFrame(-16, -16, 28, 36, 59, 227)
		SpriteFrame(-12, -24, 24, 46, 88, 217)
		SpriteFrame(-12, -24, 24, 46, 113, 217)
		SpriteFrame(-16, -20, 25, 42, 138, 221)
		SpriteFrame(-24, -12, 38, 24, 215, 229)
	end if
	CheckCurrentStageFolder("Zone00")
	if checkResult == 1
		LoadSpriteSheet("GHZ/Objects3.gif")
		SpriteFrame(-16, -16, 28, 36, 1, 227)
		SpriteFrame(-16, -16, 28, 36, 30, 227)
		SpriteFrame(-16, -16, 28, 36, 59, 227)
		SpriteFrame(-12, -24, 24, 46, 88, 217)
		SpriteFrame(-12, -24, 24, 46, 113, 217)
		SpriteFrame(-16, -20, 25, 42, 138, 221)
		SpriteFrame(-24, -12, 38, 24, 215, 229)
	end if
	foreach (TypeName[Burrobot], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value0 = object[arrayPos0].xpos
		object[arrayPos0].value1 = object[arrayPos0].ypos
		object[arrayPos0].value6 = 1
	next
	SetTableValue(TypeName[Burrobot], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Burrobot_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Burrobot_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
